package com.jidong.test_springboot.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jidong.test_springboot.dao.LeaveDao;
import com.jidong.test_springboot.entity.Leave;
import com.jidong.test_springboot.module.LeaveModal;
import com.jidong.test_springboot.util.JWTUtil;
import com.jidong.test_springboot.vo.ResultData;

@RestController
@RequestMapping("my/leave/")
public class MyLeaveServlet {
	@Value(value="${jeecg.leaveProcessKey}")
	private String leaveProcessKey;//请假流程的模板的key，可能会随着公司业务扩大，该流程可能会更新
	
	@Autowired
	private RuntimeService runtimeSerivce;
	@Autowired
	private LeaveDao leaveDao;

	@RequestMapping("add.do")
	public ResultData add(LeaveModal modal,HttpServletRequest request) {
		//读出请求头中的token
		String token=request.getHeader("Access-Token");
		//解析token得到用户名
		String username=(String)JWTUtil.getClaim(token, "username");
		Map<String ,Object> vars=new HashMap();
		vars.put("days", modal.getDays());
		vars.put("userName", username);//设置申请人用户名
		ProcessInstance pi=runtimeSerivce.startProcessInstanceByKey(leaveProcessKey,vars);
		
		Leave leave=new Leave();
		leave.setTitle(modal.getTitle());
		leave.setContent(modal.getContent());
		leave.setDays(modal.getDays());
		leave.setStatus("待提交");
		leave.setProcessInstanceId(pi.getProcessInstanceId());//把实例和业务表数据一一对相应
		
		leaveDao.insert(leave);
		
		return new ResultData().setMsg("申请成功");
	}
}
